
export let vertexSHADER = `
attribute vec4 a_Position;
void main(){
    vec4 posi=a_Position; 
    gl_Position=posi;
}
`

export let fragmentSHADER = `
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_Sampler;//数据纹理
uniform sampler2D u_Sampler1;//经纬度纹理
uniform sampler2D u_Sampler2;//色带纹理

float getValue_wind(vec4 colo) {
    float _r = colo.r * 255.;
    float _g = colo.g * 255.;
    float _b = colo.b * 255.;
    float _u = 0.;
    if(_b >= 10.) {
        _u = _r / 10.;
    } else {
        _u = -(_r / 10.);
    }
    float _v = 0.;
    if(mod(_b, 10.) == 1.) {
        _v = _g / 10.;
    } else {
        _v = -(_g / 10.);
    }
    float _val = sqrt(_u * _u + _v * _v);
    return _val;
}

float getValue_wave(vec4 colo) {
    float _r = colo.r * 255.;
    float _g = colo.g * 255.;
    float _b = colo.b * 255.;
    float _a = colo.a * 255.;
    float _val = _r + _g / 100.;
    return _val;
}

float getValue_tide(vec4 colo) {
    float _r = colo.r * 255.;
    float _g = colo.g * 255.;
    float _b = colo.b * 255.;
    float _a = colo.a * 255.;
    float _val = _r + _g / 100. + _b / 10000.;
    _val = _a < 255. ? -_val : _val;
    _val = _a == 0. ? 9999. : _val;
    return _val * 100.;
}

uniform vec4 dataRange;//数据范围
uniform float dataType;//数据类型

uniform float u_Scale;//数据分辨率
uniform float v_Scale;//数据分辨率
uniform float u_Startlon;//数据起始经度
uniform float u_Startlat;//数据起始纬度

uniform float pic_width;//纹理宽度
uniform float pic_height;//纹理高度

uniform float leftlon;//屏幕左侧经度
uniform float lonspan;//经度跨度
uniform vec2 widhei;//屏幕宽高

uniform float _Alpha;//图层透明度
uniform float minValue;//数据极大值
uniform float maxValue;//数据极小值

void main() {

    float lon = leftlon + lonspan * (gl_FragCoord.x / widhei.x);//经度
    float trueLon = lon;
    lon = mod(lon, 360.);
    if(lon < 0.) {
        lon = lon + 360.;
    }

    vec2 texturecoor = vec2(0., gl_FragCoord.y / widhei.y);
    vec4 color1 = texture2D(u_Sampler1, texturecoor);
    float lat = color1.x * 255. + color1.y * 2.55 + color1.z * .0255;//纬度
    if(color1.w > 0.) {
        lat = lat * -1.;
    }

    vec2 v_Lonlat = vec2(lon, lat);//经纬度
    float col_left = float(floor((lon - u_Startlon) / u_Scale));//在数据中的行列号
    float col_right = col_left + 1.;
    float row_top = float(floor((u_Startlat - lat) / v_Scale));
    float row_bottom = row_top + 1.;

    float left_p = col_left / pic_width;//在纹理中的行列号
    float right_p = col_right / pic_width;
    float top_p = 1. - (row_top / pic_height);
    float bottom_p = 1. - (row_bottom / pic_height);

    vec4 color_lt = texture2D(u_Sampler, vec2(left_p, top_p));//在纹理中的值
    vec4 color_rt = texture2D(u_Sampler, vec2(right_p, top_p));
    vec4 color_lb = texture2D(u_Sampler, vec2(left_p, bottom_p));
    vec4 color_rb = texture2D(u_Sampler, vec2(right_p, bottom_p));

    float val_lt = getValue_wind(color_lt);
    float val_rt = getValue_wind(color_rt);
    float val_lb = getValue_wind(color_lb);
    float val_rb = getValue_wind(color_rb);
    if(dataType == 1.) {//1为风场
        val_lt = getValue_wind(color_lt);
        val_rt = getValue_wind(color_rt);
        val_lb = getValue_wind(color_lb);
        val_rb = getValue_wind(color_rb);
    } else if(dataType == 2.) {//2为海浪
        val_lt = getValue_wave(color_lt);
        val_rt = getValue_wave(color_rt);
        val_lb = getValue_wave(color_lb);
        val_rb = getValue_wave(color_rb);
    } else if(dataType == 3.) { //潮汐风暴増水
        val_lt = getValue_tide(color_lt);
        val_rt = getValue_tide(color_rt);
        val_lb = getValue_tide(color_lb);
        val_rb = getValue_tide(color_rb);
    }

    vec2 left_top = vec2(u_Startlon + u_Scale * col_left, u_Startlat - v_Scale * row_top);
    vec2 right_top = vec2(u_Startlon + u_Scale * col_right, u_Startlat - v_Scale * row_top);
    vec2 left_bottom = vec2(u_Startlon + u_Scale * col_left, u_Startlat - v_Scale * row_bottom);

    float y1 = left_top.y - v_Lonlat.y;
    float y2 = v_Lonlat.y - left_bottom.y;
    float oo = left_top.y - left_bottom.y;

    float r_1 = y1 / v_Scale * val_lb + y2 / v_Scale * val_lt;
    float r_2 = y1 / v_Scale * val_rb + y2 / v_Scale * val_rt;
    float v_12 = (right_top.x - v_Lonlat.x) / u_Scale * r_1 + (v_Lonlat.x - left_top.x) / u_Scale * r_2;

    if(abs(right_top.x - left_top.x - u_Scale) > 0.01 || abs(oo - v_Scale) > 0.01) {
        v_12 = val_lt;
    };

    vec4 necolor = vec4(0., 0., 0., 0.);
    necolor = texture2D(u_Sampler2, vec2(0., 1. - (v_12 - minValue) / (maxValue - minValue)));

    necolor.w = _Alpha;
    if(lon < dataRange.z || lon > dataRange.w || lat < dataRange.y || lat > dataRange.x) {
        necolor = vec4(0., 0., 0., 0.);
    }
    if(dataType == 1. && v_12 <= 0.0) { //风场
        necolor = vec4(0., 0., 0., 0.);
    }
    if(dataType == 2. && v_12 <= 0.0) { //海浪
        necolor = vec4(0., 0., 0., 0.);
    }
    if(dataType == 3. && (val_lt > 990. || val_rt > 999. || val_lb > 999. || val_rb > 999.)) { //増水
        necolor = vec4(0., 0., 0., 0.);
    }
    gl_FragColor = necolor;
}
`